<template>
  <div class="announcement-page">
    <!-- 顶部导航栏 -->
    <Navbar />

    <div class="container">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1 class="page-title">
          <el-icon :size="32" class="title-icon"><Bell /></el-icon>
          公告中心
        </h1>
      </div>

      <!-- 筛选区域 -->
      <div class="filter-section">
        <el-form :inline="true" :model="filterForm" class="filter-form">
          <el-form-item label="公告类型">
            <el-select v-model="filterForm.announcementType" placeholder="全部类型" clearable @change="handleFilter">
              <el-option label="全部" value="" />
              <el-option label="系统公告" value="0" />
              <el-option label="活动公告" value="1" />
              <el-option label="重要通知" value="2" />
            </el-select>
          </el-form-item>
          <el-form-item label="置顶">
            <el-select v-model="filterForm.isTop" placeholder="全部" clearable @change="handleFilter">
              <el-option label="全部" value="" />
              <el-option label="置顶" value="1" />
              <el-option label="非置顶" value="0" />
            </el-select>
          </el-form-item>
          <el-form-item label="标题搜索">
            <el-input 
              v-model="filterForm.title" 
              placeholder="请输入公告标题" 
              clearable 
              @keyup.enter="handleFilter"
              style="width: 240px"
            >
              <template #append>
                <el-button :icon="Search" @click="handleFilter" />
              </template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>

      <!-- 公告列表 -->
      <div class="announcement-list" v-loading="loading">
        <el-empty v-if="!loading && announcementList.length === 0" description="暂无公告" />

        <div 
          v-for="announcement in announcementList" 
          :key="announcement.announcementId"
          class="announcement-item"
          :class="{ 'is-top': announcement.isTop === '1' }"
          @click="goToDetail(announcement.announcementId)"
        >
          <div class="announcement-header">
            <div class="announcement-title-wrapper">
              <el-tag v-if="announcement.isTop === '1'" type="danger" size="small" effect="dark">
                置顶
              </el-tag>
              <el-tag :type="getTypeTag(announcement.announcementType)" size="small">
                {{ getTypeName(announcement.announcementType) }}
              </el-tag>
              <h3 class="announcement-title">{{ announcement.title }}</h3>
            </div>
            <div class="announcement-meta">
              <span class="meta-item">
                <el-icon><User /></el-icon>
                {{ announcement.publisherName }}
              </span>
              <span class="meta-item">
                <el-icon><View /></el-icon>
                {{ announcement.viewCount }}
              </span>
              <span class="meta-item">
                <el-icon><Clock /></el-icon>
                {{ announcement.publishTime }}
              </span>
            </div>
          </div>
          <div class="announcement-content">
            {{ truncateContent(announcement.content) }}
          </div>
        </div>
      </div>

      <!-- 分页 -->
      <el-pagination
        v-if="total > 0"
        class="pagination"
        v-model:current-page="pageNum"
        v-model:page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 30, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { getAnnouncementList } from '@/api/announcement'
import { Bell, Search, User, View, Clock } from '@element-plus/icons-vue'
import Navbar from '@/components/Navbar.vue'

const router = useRouter()

// 筛选表单
const filterForm = ref({
  title: '',
  announcementType: '',
  isTop: ''
})

// 列表数据
const announcementList = ref([])
const loading = ref(false)
const pageNum = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 获取公告列表
const loadAnnouncementList = async () => {
  loading.value = true
  try {
    const data = await getAnnouncementList({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      ...filterForm.value
    })
    announcementList.value = data.list || []
    total.value = data.total || 0
  } catch (error) {
    console.error('获取公告列表失败:', error)
  } finally {
    loading.value = false
  }
}

// 筛选
const handleFilter = () => {
  pageNum.value = 1
  loadAnnouncementList()
}

// 分页
const handleSizeChange = () => {
  loadAnnouncementList()
}

const handleCurrentChange = () => {
  loadAnnouncementList()
}

// 跳转到详情
const goToDetail = (announcementId) => {
  router.push(`/announcement/${announcementId}`)
}

// 获取类型名称
const getTypeName = (type) => {
  const typeMap = {
    '0': '系统公告',
    '1': '活动公告',
    '2': '重要通知'
  }
  return typeMap[type] || '未知'
}

// 获取类型标签颜色
const getTypeTag = (type) => {
  const typeMap = {
    '0': 'info',
    '1': 'success',
    '2': 'warning'
  }
  return typeMap[type] || 'info'
}

// 截断内容
const truncateContent = (content) => {
  if (!content) return ''
  return content.length > 150 ? content.substring(0, 150) + '...' : content
}

onMounted(() => {
  loadAnnouncementList()
})
</script>

<style scoped>
.announcement-page {
  width: 100%;
  min-height: 100vh;
  background-color: #f5f5f5;
  margin: 0;
  padding: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
}

.page-header {
  margin-bottom: 30px;
}

.page-title {
  font-size: 32px;
  font-weight: bold;
  color: #333;
  display: flex;
  align-items: center;
  gap: 12px;
}

.title-icon {
  color: #ff4d4f;
}

.filter-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.filter-form {
  margin: 0;
}

.announcement-list {
  min-height: 400px;
}

.announcement-item {
  background: white;
  border-radius: 8px;
  padding: 24px;
  margin-bottom: 16px;
  cursor: pointer;
  transition: all 0.3s;
  border: 2px solid transparent;
}

.announcement-item:hover {
  border-color: #ff4d4f;
  box-shadow: 0 4px 12px rgba(255, 77, 79, 0.15);
  transform: translateY(-2px);
}

.announcement-item.is-top {
  border-left: 4px solid #ff4d4f;
  background: linear-gradient(to right, #fff5f5, #ffffff);
}

.announcement-header {
  margin-bottom: 12px;
}

.announcement-title-wrapper {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.announcement-title {
  font-size: 18px;
  font-weight: bold;
  color: #333;
  margin: 0;
  flex: 1;
}

.announcement-meta {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #999;
}

.meta-item .el-icon {
  font-size: 16px;
}

.announcement-content {
  font-size: 14px;
  color: #666;
  line-height: 1.8;
  text-align: justify;
}

.pagination {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  padding-bottom: 20px;
}

@media (max-width: 768px) {
  .page-title {
    font-size: 24px;
  }

  .filter-form {
    display: flex;
    flex-direction: column;
  }

  .filter-form .el-form-item {
    margin-right: 0;
    margin-bottom: 16px;
  }

  .announcement-title {
    font-size: 16px;
  }

  .announcement-meta {
    gap: 12px;
  }
}
</style>
